package org.dzms.commons;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class DbManager {
	private  Connection conn;
	private  PreparedStatement stmt;
	private  ResultSet rs;
	public DbManager() {
		conn = DbUtil.getConnection();
	}
	
	public int update(String sql, Object[] params) {
		int count = 0;
		try {
			stmt = conn.prepareStatement(sql);
			if (params != null && params.length != 0) {
				for (int i = 0; i < params.length; i++) {
					stmt.setObject(i+1, params[i]);
				}
			}
			count = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public ResultSet query(String sql, Object[] params) {
		rs = null;
		try {
			stmt = conn.prepareStatement(sql);
			if (params != null && params.length != 0) {
				for (int i = 0; i < params.length; i++) {
					stmt.setObject(i+1, params[i]);
				}
			}
			rs = stmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public void close() {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
